package manager.action;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import manager.actionform.TeacherForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import dao.TeacherMagDao;
import dao.impl.TeacherMagImpl;
import domain.Teacher;

/**
 * 
 * DispatchAction，管理员管理老师Action
 * 
 * May 10, 2009
 *
 * @author Fabian
 *
 */
public class ManagerTeacherAction extends DispatchAction {

	/**
	 * 浏览所有老师的Action，但是要传递分页参数（start--开始，limit--间隔）
	 */
	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		TeacherForm tf = (TeacherForm)form;
		int start = tf.getStart();
		int limit = tf.getLimit();		
		
		TeacherMagDao tmd = new TeacherMagImpl();
		//获取此页面的老师
		List<Teacher> list = tmd.getPageTeacher(start, limit);
		//获取总记录数
		int totalCount = tmd.getTotalCount();
		
		//组装JSON字符串，返回
		String resultArray = "";
		for (Iterator<Teacher> it = list.iterator(); it.hasNext(); ) {
			Teacher th = it.next();
			resultArray = resultArray + "{id: " + th.getId() + ", name: '" + th.getName() +
							"', sex: '" + th.getSex() + "', nickname: '" + th.getNickname() +
							"', password: '" + th.getPassword() + "', major:'" + th.getMajor() +
							"', major_id: " + th.getMajorId() + "}";
			if (it.hasNext()) {
				resultArray = resultArray + ",";
			}			
		}
		
		
		String jsonStr = "";
		jsonStr = "{totalCount: " + totalCount + ", result: [ " + resultArray + " ]}";
		
		request.setAttribute("result", jsonStr);
				
		return mapping.findForward("show_result");
	}
	
	/**
	 * 删除老师，要传递老师的id
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		TeacherForm tf = (TeacherForm)form;
		//获取删除传递到后台的id集合字符串
		String delIdStr = tf.getDelStr();
		
		//删除字符串中的id组的所有老师
		TeacherMagDao tmd = new TeacherMagImpl();
		boolean result = tmd.delTeachers(delIdStr);
		
		//组装返回的JSON数据
		String returnStr = "";
		if (result) {
			
			returnStr = "{result: 'success', msg: '恭喜，删除老师成功！'}";
			request.setAttribute("result", returnStr);
			
		} else {
			
			returnStr = "{result: 'failure', msg: '抱歉，删除老师失败！请重新尝试！'}";
			request.setAttribute("result", returnStr);
			
		}		
		
		return mapping.findForward("del_result");
	}
	
	/**
	 * 添加老师，要传递老师的信息
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		TeacherForm tf = (TeacherForm)form;
		//姓名
		String name = tf.getName();
		//性别
		String sex = tf.getSex();
		//登陆名
		String nickname = tf.getNickname();
		//密码
		String password = tf.getPassword();
		//专业id
		int majorId = tf.getMajorId();
		
		Teacher t = new Teacher();
		t.setName(name);
		t.setSex(sex);
		t.setNickname(nickname);
		t.setPassword(password);
		t.setMajorId(majorId);
		
		//添加新老师
		TeacherMagDao tmd = new TeacherMagImpl();
		boolean result = tmd.addTeacher(t);
		
		//组装返回的JSON数据
		String returnStr = "";
		if (result) {
			
			returnStr = "{success: true, msg: '恭喜，添加老师成功！'}";
			request.setAttribute("result", returnStr);
			
		} else {
			
			returnStr = "{failure: true, msg: '抱歉，添加老师失败！请重新尝试！'}";
			request.setAttribute("result", returnStr);
			
		}		
		return mapping.findForward("add_result");
	}
	
	/**
	 * 修改更新老师数据，要传递老师的id
	 */
	public ActionForward modify(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		TeacherForm tf = (TeacherForm)form;
		//编号
		int id = tf.getId();
		//姓名
		String name = tf.getName();
		//性别
		String sex = tf.getSex();
		//登陆名
		String nickname = tf.getNickname();
		//密码
		String password = tf.getPassword();
		//专业id
		int majorId = tf.getMajorId();
		
		Teacher t = new Teacher();
		t.setId(id);
		t.setName(name);
		t.setSex(sex);
		t.setNickname(nickname);
		t.setPassword(password);
		t.setMajorId(majorId);
		
		//添加新老师
		TeacherMagDao tmd = new TeacherMagImpl();
		boolean result = tmd.modifyTeacher(t);
		
		//组装返回的JSON数据
		String returnStr = "";
		if (result) {
			
			returnStr = "{success: true, msg: '恭喜，修改老师信息成功！'}";
			request.setAttribute("result", returnStr);
			
		} else {
			
			returnStr = "{failure: true, msg: '抱歉，修改老师信息失败！请重新尝试！'}";
			request.setAttribute("result", returnStr);
			
		}		
		return mapping.findForward("modify_result");
	}	
}
